package com.oig.sys.workflow.service.impl;

import com.oig.common.constants.CommonEnum;
import com.oig.common.exception.BadRequestException;
import com.oig.sys.workflow.dao.ITbFlowConfigDao;
import com.oig.sys.workflow.service.ITbFlowConfigService;
import com.oig.sys.workflow.entity.TbFlowConfig;
import com.oig.sys.workflow.dto.FlowConfigDto;
import com.oig.sys.workflow.service.ITbFlowFormService;
import com.oig.sys.workflow.vo.FlowConfigVo;

import com.oig.sys.workflow.vo.FlowFormVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author wuxl
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class TbFlowConfigServiceImpl implements ITbFlowConfigService {

	private final ITbFlowConfigDao tbFlowConfigDao;
	private final ITbFlowFormService flowFormService;


	@Transactional
	@Override
	public FlowConfigVo addFlowConfig(FlowConfigDto flowConfigDto){
		TbFlowConfig flowConfig = this.tbFlowConfigDao.getByBizType(flowConfigDto.getBizType());
		if (flowConfig!=null){
			throw new BadRequestException("业务编码重复");
		}
		flowConfig = this.tbFlowConfigDao.getByFlowCode(flowConfigDto.getFlowCode());
		if (flowConfig!=null){
			throw new BadRequestException("流程编码重复");
		}
		flowConfig = new TbFlowConfig();
		BeanUtils.copyProperties(flowConfigDto, flowConfig,"id");
		if (StringUtils.isBlank(flowConfig.getType())){
			flowConfig.setType("0");
		}
		flowConfig.setStatus(CommonEnum.CommonStatusEnum.OK.getCode());
		flowConfig.setSubscribed(CommonEnum.FlagEnum.FALSE_FLAG.getCode());
		this.tbFlowConfigDao.save(flowConfig);
		List<FlowFormVo> flowFormVoList = this.flowFormService.addFlowFormList(flowConfig, null, flowConfigDto.getFlowFormDtoList());
		FlowConfigVo flowConfigVo = new FlowConfigVo();
		BeanUtils.copyProperties(flowConfig, flowConfigVo);
		flowConfigVo.setFlowFormVoList(flowFormVoList);
		return flowConfigVo;
	}

	@Transactional
	@Override
	public FlowConfigVo updateFlowConfig(FlowConfigDto flowConfigDto){
		TbFlowConfig flowConfig = this.tbFlowConfigDao.getTbFlowConfigById(flowConfigDto.getId());
		if (!CommonEnum.CommonStatusEnum.OK.getCode().equals(flowConfig.getStatus())){
			throw new BadRequestException("已删除") ;
		}
		BeanUtils.copyProperties(flowConfigDto, flowConfig,"id","createUserId","createUserName","createTime","tenantId","tenantName");
		this.tbFlowConfigDao.save(flowConfig);
		List<FlowFormVo> flowFormVoList = this.flowFormService.updateFlowFormList(flowConfig, null, flowConfigDto.getFlowFormDtoList());
		FlowConfigVo flowConfigVo = new FlowConfigVo();
		BeanUtils.copyProperties(flowConfig, flowConfigVo);
		flowConfigVo.setFlowFormVoList(flowFormVoList);
		return flowConfigVo;
	}

	@Transactional
	@Override
	public void updateSubscribe(Integer id, String flag) {
		TbFlowConfig flowConfig = this.tbFlowConfigDao.getTbFlowConfigById(id);
		flowConfig.setSubscribed(flag);
		this.tbFlowConfigDao.save(flowConfig);
	}

	@Transactional
	@Override
	public void cancelFlowConfig(Integer id){
		TbFlowConfig flowConfig = this.tbFlowConfigDao.getTbFlowConfigById(id);
		if (CommonEnum.CommonStatusEnum.DEL.getCode().equals(flowConfig.getStatus())){
			throw new BadRequestException("已是注销状态") ;
		}
		flowConfig.setStatus(CommonEnum.CommonStatusEnum.DEL.getCode());
		this.tbFlowConfigDao.save(flowConfig);
	}


	@Override
	public FlowConfigVo getFlowConfig(Integer id) {
		TbFlowConfig flowConfig = this.tbFlowConfigDao.getTbFlowConfigById(id);
		FlowConfigVo flowConfigVo = new FlowConfigVo();
		BeanUtils.copyProperties(flowConfig, flowConfigVo);
		flowConfigVo.setFlowFormVoList(this.flowFormService.findFlowFormByCfgId(id, 0));
		return flowConfigVo;
	}

	@Override
	public FlowConfigVo getFlowConfigByBizType(String bizType) {
		TbFlowConfig flowConfig = this.tbFlowConfigDao.getByBizType(bizType);
		FlowConfigVo flowConfigVo = new FlowConfigVo();
		BeanUtils.copyProperties(flowConfig, flowConfigVo);
		flowConfigVo.setFlowFormVoList(this.flowFormService.findFlowFormByCfgId(flowConfig.getId(), 0));
		return flowConfigVo;
	}

}
